import * as SDKQG from '@datarangers/sdk-qg';
const ensureImport = <T>(raw: T): T =>
    typeof (raw as any).default === 'object' ? (raw as any).default : raw;
globalThis.$$Rangers = ensureImport(SDKQG)

import { _decorator, AudioClip, Component, Node, SpriteFrame, sys, tween, v3, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass
export default class TAComponent {
    static init() {

    }
    static identify() { }
    static getDistinctId() { return '' }
    static getDeviceId() {
        return globalThis.ssid;
    }
    static trackFirstEvent() { }
    static track(event: string, params) {
        if (params?.hasOwnProperty('scene')) {
            params['game_scene'] = params['scene'];
            delete params['scene'];
        }
        globalThis.$$Rangers.event(event, params);
    }
    static setSuperProperties(params) {
        globalThis.$$Rangers.config(params);
    }
    static userSetOnce(params) {
        globalThis.$$Rangers.profileSetOnce(params);
    }
    static userSet(params) {
        globalThis.$$Rangers.profileSet(params);
    }
    static login(id: string) {
        console.log("登录埋点服务器---------------------", id);

        const isFirst = !localStorage.getItem('user_unique_id')
        if (isFirst) {
            localStorage.setItem('user_unique_id', id)
        }
        globalThis.$$Rangers.config({ user_unique_id: id });
        globalThis.$$Rangers.event('user_login', { is_first: isFirst })

        console.log("登录埋点服务器---------------------", id);
    }

    public static app_id = {
        tt: 10000072,
        ks: 10000064
        // ks: 10000059
    };
    public static app_name = {
        tt: "搞怪砖家",
        ks: "消消更养生",
        // ks: "养生消消消",
    };

    static Init() {
        let app_id = TAComponent.app_id.ks; // 10000000;
        let app_name = TAComponent.app_name.ks;

        const isKSGame = typeof window['KSGameGlobal'] != 'undefined';
        // 抖音平台
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            app_id = TAComponent.app_id.tt;
            app_name = TAComponent.app_name.tt;

            console.log("抖音");
        }
        // 快手平台
        else if (isKSGame) {
            app_id = TAComponent.app_id.ks;
            app_name = TAComponent.app_name.ks;

            console.log("快手");
        }
        else {
            console.log("未知平台");
        }

        console.log("app_id-------------------------", app_id);
        console.log("app_name-------------------------", app_name);

        // 初始化
        globalThis.$$Rangers.init({
            app_id: app_id, // 替换成申请的app_id，注意类型是number而非字符串 【联系对接人获取】
            channel_domain: "https://snssdk.bistream.net", // 设置数据上送域名 【联系对接人获取】
            log: true, // 开启后会控制台会打印日志，开发阶段有助于查看埋点上报过程
            // log: false, // 开启后会控制台会打印日志，开发阶段有助于查看埋点上报过程
            auto_report: false, // 开启后会上报一些预定义事件，如app_launch、app_terminate等
            enable_skip_launch: true // 跳过launch预置事件的上报，解决某些场景下的异常
        });

        // 设置应用信息
        globalThis.$$Rangers.config({
            mp_name: app_name, // 小游戏实际名称
            app_version: '1.1.1' // 小游戏实际版本
        });

        // 初始化设置完毕之后，必须调用send方法，sdk才真正初始化完毕，之前不会有数据上报
        globalThis.$$Rangers.send();


        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            tt.onShow((res) => {
                onShowedTime = Date.now()
                globalThis.$$Rangers.event('game_launch', { from_background: 1, is_first: 0, launch_scene: res.scene })
            })
            tt.onHide(() => {
                globalThis.$$Rangers.event('game_terminate', { stay_duration: (Date.now() - onShowedTime) / 1000 })
            })
            let onShowedTime = Date.now()
            globalThis.$$Rangers.event('game_launch', { from_background: 0, is_first: !localStorage.getItem('user_unique_id'), launch_scene: tt.getLaunchOptionsSync().scene })
        }

        if (isKSGame) {
            ks.onShow((res) => {
                onShowedTime = Date.now()
                globalThis.$$Rangers.event('game_launch', { from_background: 1, is_first: 0, launch_scene: res.scene })
            })
            ks.onHide(() => {
                globalThis.$$Rangers.event('game_terminate', { stay_duration: (Date.now() - onShowedTime) / 1000 })
            })
            let onShowedTime = Date.now()
            globalThis.$$Rangers.event('game_launch', { from_background: 0, is_first: !localStorage.getItem('user_unique_id'), launch_scene: ks.getLaunchOptionsSync().scene })
        }
    }

    // 上报事件
    // 事件名： event_name
    // 事件属性： event_props
    static SendEvent(event_name: string, event_props: any) {
        return
        globalThis.$$Rangers.event(event_name, event_props);
    }

    // protected onLoad(): void {
    //     // return
    //     if (cc.sys.platform == cc.sys.BYTEDANCE_GAME || cc.sys.platform == cc.sys.WECHAT_GAME) {
    //         let app_id = cc.sys.platform == cc.sys.BYTEDANCE_GAME ? 10000020 : 10000025
    //         if (typeof window['KSGameGlobal'] != 'undefined') {
    //             app_id = 10000021
    //         }
    //         globalThis.$$Rangers.init({
    //             app_id,
    //             channel_domain: `https://snssdk.bistream.net`,
    //             log: true,
    //             auto_report: true,
    //             enable_profile: true,
    //             enable_skip_launch: true,
    //         })
    //         globalThis.$$Rangers.send()
    //         if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
    //             tt.onShow((res) => {
    //                 onShowedTime = Date.now()
    //                 globalThis.$$Rangers.event('game_launch', { from_background: 1, is_first: 0, launch_scene: res.scene })
    //             })
    //             tt.onHide(() => {
    //                 globalThis.$$Rangers.event('game_terminate', { stay_duration: (Date.now() - onShowedTime) / 1000 })
    //             })
    //         }
    //         let onShowedTime = Date.now()
    //         globalThis.$$Rangers.event('game_launch', { from_background: 0, is_first: !localStorage.getItem('user_unique_id'), launch_scene: tt.getLaunchOptionsSync().scene })
    //     } else {
    //     }

    //     if (cc.sys.platform == cc.sys.BYTEDANCE_GAME || (cc.sys.platform == cc.sys.WECHAT_GAME && typeof window['KSGameGlobal'] == 'undefined'))
    //         globalThis.$$Rangers.getToken().then(value => globalThis.ssid = value.ssid);
    //     // TAComponent.api.init();
    // }

}
